var User;
var securityCode;
var sellenter = new Vue({
    el: '#sellenter',
    //初始化数据类型()
    data: {
        //获取到的验证码信息
        securityCode: '',
        //用户填写的验证码
        securityCodeUser: '',
        options: [],
        button:'',
        optionsUser:[],
        Merchant: {},
        User:[],
        importFileUrl: '/home/merchant/insertALL',
    },
    //提交时间方法
    methods: {
        //获取验证码信息
        /*mailCode: function () {
            var _this = this;
            $.post("/home/merchant/mailCode", {"mail": _this.Merchant.mail}, function (data) {
                if (data != "500") {
                    _this.securityCode = data;
                    _this.$message.success("如果您的邮箱中没有本次操作的邮件信息，请查看邮箱中的垃圾箱，并把它标记为不是垃圾邮件!");
                } else {
                    _this.$message.error("获取验证码失败！");
                }


            });

        },*/
        //获取手机验证码
        telCode:function(){
            var _this = this;
            $("#input7").attr("disabled","disabled");
            $.post("/home/warnInfo/sendInfo",{"tel":_this.Merchant.tel},function(data){
                if (data != "500") {
                    _this.securityCode = data;
                    _this.$message.success("验证码已发送!");
                    sendMessage();
                } else {
                    _this.$message.error("获取验证码失败！");
                }
            });
        },

        sellsenter: function () {
            debugger
            $("#submit").attr("disabled", "disabled");
            var _this = this;
            _this.Merchant.affiliatedRegional =  $("#province1").val() + "-" + $("#city1").val() + "-" + $("#district1").val();
            _this.$refs.upload.submit();
        },
        //文件上传成功后的回调函数
        handleAvatarSuccess: function (response, file, fileList) {
            if (response == "入驻信息提交成功!") {
                this.$message.success(response);
            } else {
                this.$message.error("入驻信息提交失败");
                $("#submit").removeAttr("disabled");
            }

        },
        //文件上传失败的回调函数
        handleAvatarError: function (err, file, fileList) {
            this.$message.error("失败！" + err);
            $("#submit").removeAttr("disabled");
        },
        // 上传前对文件的大小的判断
        /*beforeAvatarUpload :function(file) {
            const extension = file.name.split('.')[1] === 'xls'
            const extension2 = file.name.split('.')[1] === 'xlsx'
            const extension3 = file.name.split('.')[1] === 'doc'
            const extension4 = file.name.split('.')[1] === 'docx'
            const isLt2M = file.size / 1024 / 1024 < 10
            if (!extension && !extension2 && !extension3 && !extension4) {
                alert('上传模板只能是 xls、xlsx、doc、docx 格式!')
            }
            if (!isLt2M) {
                alert('上传模板大小不能超过 10MB!')
            }
            return extension || extension2 || extension3 || extension4 && isLt2M
        }*/

    },

    //页面加载完成后执行此方法
    created: function () {
        var _this = this;
        $.post("/home/menu/select", "", function (data) {
            for (var i = 0; i < data.data.Menu.length; i++) {
                var map1 = {};
                map1.children = [];
                map1.value = data.data.Menu[i].id;
                map1.label = data.data.Menu[i].name;
                _this.options.push(map1);
            }
        });

        $.post("/sys/user/seleceOfficeUser", "", function (data) {
            for (var i = 0; i < data.data.sysUser.length; i++) {
                debugger
                var map1 = {};
                map1.value = data.data.sysUser[i].id;
                map1.label = data.data.sysUser[i].name;
                _this.optionsUser.push(map1);
            }
        });


    }

});


//表单验证信息
//file表单数据赋予input[type="text"]
$(".el-upload__input").change(function () {
    var see = $(".el-upload__input").val()
    $("#uploadFile").val(see);
});


//页面加载完成执行此程序
$(function () {
    $("#submit").attr("disabled", "disabled");
    $("#code").attr("disabled","disabled");
//查询用户表
    $.post("/sys/user/selectUserForm","",function(data){
       User = data.data.sysUser;
    });
});

//提交按钮状态设置
function enableSubmit(bool) {
    if (bool) {
        $("#submit").removeAttr("disabled");
    } else {
        $("#submit").attr("disabled", "disabled");
    }
}

//验证多少项，多少个false
var flags = [false, false, false, false, false, false, false, false, false, false, false];

//flags全为true时提交按钮解除禁用
function v_submitbutton() {
    for (f in flags) if (!flags[f]) {
        enableSubmit(false);
        return;
    }
    enableSubmit(true);
}

//帐号验证
var RegUserName = /^[a-zA-Z0-9_-]{4,16}$/;
//邮箱验证的正则
var RegEmail = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
//手机号码验证
var RegTel = /^1[0-9]\d{9}$/;
//身份证号验证
var RegidCardNo = /^[0-9]{17}(\d|[xX])|[0-9]{14}(\d|[xX])$/;
//组织机构代码验证
var ReginstitutionCode = /^[0-9]{9}$/;
//图片验证
var RegComPanyName = /^\S+\.(jpg|png|)$/;

//验证帐号
function v_username() {
    var username = $("#input1").val();
    var arr = [];
    for(var i=0;i<User.length;i++){
        arr.push(User[i].username);
    }
    if($.inArray(username, arr)>=0){
        flags[0] = false;
        enableSubmit(false);
        $("#tab1").html(" &nbsp;帐号已存在");
        $("#img-icon1-2").css("display", "block");
        $("#tab1").css("display", "block");
        $("#input1").css("border", "solid 1px red");
    }else{
        if(!RegUserName.test(username)){
            flags[0] = false;
            enableSubmit(false);
            $("#tab1").html(" &nbsp;帐号格式不正确");
            $("#img-icon1-2").css("display", "block");
            $("#tab1").css("display", "block");
            $("#input1").css("border", "solid 1px red");
        }else{
            flags[0] = true;
            $("#input1").css("border", "solid 1px #2dc12d", "");
            $("#img-icon1-1").css("display", "block");
            $("#tab1").css("display", "none");
            $("#img-icon1-2").css("display", "none");
        }
    }
    /*if (!RegUserName.test(username)) {
        flags[0] = false;
        enableSubmit(false);
        $("#tab1").html(" &nbsp;帐号格式错误");
        $("#img-icon1-2").css("display", "block");
        $("#tab1").css("display", "block");
        $("#input1").css("border", "solid 1px red");
    }
    else {
        flags[0] = true;
        $("#input1").css("border", "solid 1px #2dc12d", "");
        $("#img-icon1-1").css("display", "block");
        $("#tab1").css("display", "none");
        $("#img-icon1-2").css("display", "none");
    }*/
    v_submitbutton();
}

//验证公司名称
function v_companyname() {
    var companyname = $("#input4").val();
    if (companyname.trim() == "") {
        flags[1] = false;
        enableSubmit(false);
        $("#tab2").html("此项不能为空");
        $("#img-icon2-2").css("display", "block");
        $("#tab2").css("display", "block");
        $("#input4").css("border", "solid 1px red");
    }
    else {
        flags[1] = true;
        $("#input4").css("border", "solid 1px #2dc12d", "");
        $("#img-icon2-1").css("display", "block");
        $("#tab2").css("display", "none");
        $("#img-icon2-2").css("display", "none");
    }
    v_submitbutton();
}

//验证电话
function v_telphone() {
    var telphone = $("#input7").val();
    if (!RegTel.test(telphone)) {
        flags[2] = false;
        enableSubmit(false);
        $("#tab5").html("手机号码为11位");
        $("#img-icon5-2").css("display", "block");
        $("#tab5").css("display", "block");
        $("#input7").css("border", "solid 1px red");
        $("#code").attr("disabled","disabled");
        $("#input6").val('');
    }
    else {
        flags[2] = true;
        $("#input7").css("border", "solid 1px #2dc12d", "");
        $("#img-icon5-1").css("display", "block");
        $("#tab5").css("display", "none");
        $("#img-icon5-2").css("display", "none");
        $("#input6").val('');
        $("#code").removeAttr("disabled");

    }

    v_submitbutton();
}

//验证邮箱
function v_email() {
    var email = $("#input5").val();
    if (!RegEmail.test(email)) {
        flags[3] = false;
        enableSubmit(false);
        $("#tab3").html("邮箱格式不正确");
        $("#img-icon3-2").css("display", "block");
        $("#tab3").css("display", "block");
        $("#input5").css("border", "solid 1px red");
    }
    else {
        flags[3] = true;
        $("#input5").css("border", "solid 1px #2dc12d", "");
        $("#img-icon3-1").css("display", "block");
        $("#tab3").css("display", "none");
        $("#img-icon3-2").css("display", "none");
    }
    v_submitbutton();
}


//验证身份证号
function v_idCardNo() {
    var idCardNo = $("#input8").val();
    if (!RegidCardNo.test(idCardNo)) {
        flags[4] = false;
        enableSubmit(false);
        $("#tab6").html("身份证号错误");
        $("#img-icon6-2").css("display", "block");
        $("#tab6").css("display", "block");
        $("#input8").css("border", "solid 1px red");
    }
    else {
        flags[4] = true;
        $("#input8").css("border", "solid 1px #2dc12d", "");
        $("#img-icon6-1").css("display", "block");
        $("#tab6").css("display", "none");
        $("#img-icon6-2").css("display", "none");
    }
    v_submitbutton();
}

//验证组织机构代码
function v_institutionCode() {
    var institutionCode = $("#input9").val();
    if (institutionCode.trim()=="") {
        flags[5] = false;
        enableSubmit(false);
        $("#tab7").html("输入错误");
        $("#img-icon7-2").css("display", "block");
        $("#tab7").css("display", "block");
        $("#input9").css("border", "solid 1px red");
    }
    else {
        flags[5] = true;
        $("#input9").css("border", "solid 1px #2dc12d", "");
        $("#img-icon7-1").css("display", "block");
        $("#tab7").css("display", "none");
        $("#img-icon7-2").css("display", "none");
    }
    v_submitbutton();
}

//验证经营模式
function v_businessModel() {
    var businessModel = $("#input10").val();
    if (businessModel.trim() == "") {
        flags[6] = false;
        enableSubmit(false);
        $("#tab8").html("必输项");
        $("#img-icon8-2").css("display", "block");
        $("#tab8").css("display", "block");
        $("#input10").css("border", "solid 1px red");
    }
    else {
        flags[6] = true;
        $("#input10").css("border", "solid 1px #2dc12d", "");
        $("#img-icon8-1").css("display", "block");
        $("#tab8").css("display", "none");
        $("#img-icon8-2").css("display", "none");
    }
    v_submitbutton();
}

//验证商品类型
$(".el-input__inner").on("click", v_goodsType);

function v_goodsType() {
    if (($(".el-input__inner").attr("placeholder")) == "请选择") {
        flags[10] = false;
        enableSubmit(false);
        $("#tab12").html("必输项");
        $("#img-icon10-2").css("display", "block");
        $("#tab12").css("display", "block");
        $(".el-input__inner").css("border", "solid 1px red");
    }
    else if (($(".el-input__inner").attr("placeholder")) != "请选择") {
        flags[10] = true;
        $(".el-input__inner").css("border", "solid 1px #2dc12d", "");
        $("#img-icon10-1").css("display", "block");
        $("#tab12").css("display", "none");
        $("#img-icon10-2").css("display", "none");
    }
    v_submitbutton();
}

//验证省、市
function v_affiliatedRegional() {
    v_goodsType();
    var selected = document.getElementsByClassName("form-control select");
    for (var i = 0; i < selected.length; i++) {
        if (selected[i].value == "") {
            flags[7] = false;
            enableSubmit(false);
            $("#tab9").html("必输项");
            $("#tab9").css("display", "block");
            $("#province1,#city1,#district1").css("border", "solid 1px red");
        } else {
            flags[7] = true;
            $("#province1,#city1,#district1").css("border", "solid 1px #2dc12d", "");
            $("#tab9").css("display", "none");
        }
        v_submitbutton();
    }
}

//验证姓名

function v_xmname() {
    var xmname = $("#input11").val();
    if (xmname.trim()=="") {
        flags[10] = false;
        enableSubmit(false);
        $("#tab111").html("姓名不能为空");
        $("#img-icon11-2").css("display", "block");
        $("#tab111").css("display", "block");
        $("#input11").css("border", "solid 1px red");
    }
    else {
        flags[10] = true;
        $("#input11").css("border", "solid 1px #2dc12d");
        $("#img-icon11-1").css("display", "block");
        $("#tab111").css("display", "none");
        $("#img-icon11-2").css("display", "none");
    }
    v_submitbutton();
}

//验证复选框
function v_checkbox() {
    if ($('input[id="lebal"]').prop("checked")) {
        flags[8] = true;
        $("#tab11").css("display", "none");
    }
    else {
        flags[8] = false;
        enableSubmit(false);
        $("#tab11").html("未选中");
        $("#tab11").css("display", "block")
    }
    v_submitbutton();
}

//验证验证码

function v_code() {
    var securityCodeUser = $("#input6").val();
    if (securityCodeUser != sellenter.securityCode || securityCodeUser.trim() =="") {
        flags[9] = false;
        enableSubmit(false);
        $("#tab4").html("请输入4位验证码");
        $("#img-icon4-2").css("display", "block");
        $("#tab4").css("display", "block");
        $("#input6").css("border", "solid 1px red");
        $("#input6").removeAttr("disabled");


    }
    else {
        flags[9] = true;
        $("#input6").css("border", "solid 1px #2dc12d", "");
        $("#img-icon4-1").css("display", "block");
        $("#tab4").css("display", "none");
        $("#img-icon4-2").css("display", "none");
        $("#input7").attr("disabled","disabled");
        $("#input6").attr("disabled","disabled");
        $("#code").attr("disabled","disabled");
        window.clearInterval(InterValObj);
    }
    v_submitbutton();
}
var InterValObj; //timer变量，控制时间
var count = 120; //间隔函数，1秒执行
var curCount;//当前剩余秒数

function sendMessage() {
    curCount = count;
    //设置button效果，开始计时
    $("#code").attr("disabled", "true");
    $("#code").val(curCount+"秒");
    InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器，1秒执行一次
    //请求后台发送验证码 TODO

}
//timer处理函数
function SetRemainTime() {
    if (curCount == 0) {
        window.clearInterval(InterValObj);//停止计时器
        if(!flags[9]){
            $("#input7").removeAttr("disabled");
            $("#code").removeAttr("disabled");
        }
        $("#code").val("获取验证码");
    }
    else {
        curCount--;
        $("#code").val(curCount+"秒");
    }
}

function adaptValue() {
    return true;
}

